// document.write(location.href);
window.onbeforeunload = function(){
    return "askForClose"
}

function TestJsDialog() {
    let flag = confirm("这是一个confirm对话框 \n做出选择之后会弹出alert");
    alert("用户选择了：" + (flag?"确认":"取消"));
}

function TestPromptDialog() {
    let userInput = prompt("请输入一段文字", "这是一段文字的默认值");
    alert("用户输入了:"+ userInput);
}

let eventer = {
    dic:{},
    emit(eventName, ...obj) {
        if (!this.dic[eventName]) {
            console.warn(`没有找到该事件的监听函数：${eventName}`)
            return;
        }
        this.dic[eventName].forEach((func) => func(...obj))
    },
    on(eventName, callBack) {
        if (!this.dic[eventName]) this.dic[eventName] = [callBack]
        else this.dic[eventName].push(callBack)
    },
    once(eventName, callBack) {
        let callBackWrap = (...obj) => {
            let index = this.dic[eventName].findIndex((v) => v == callBackWrap)
            if (index >= 0) this.dic[eventName].splice(index, 1)
            callBack(...obj)
        }
        if (!this.dic[eventName]) this.dic[eventName] = [callBackWrap]
        else this.dic[eventName].push(callBackWrap)
    },
    off(eventName, callBack) {
        if (!this.dic[eventName] || this.dic[eventName].length < 1) return
        if (!callBack) {
            delete this.dic[eventName]
            return
        }
        let index = this.dic[eventName].findIndex((v) => v == callBack)
        if (index >= 0) this.dic[eventName].splice(index, 1)
    }
}

let browserWindow = {
    getMsgName(args) {
        return `window_${args}`
    },
    minimize() {
        alert("success");
        let msgName = this.getMsgName('minimize');
        alert("test"+msgName);
        window.nativeCall(msgName);
    },
    maximize() {
        let msgName = this.getMsgName('maximize');
        window.nativeCall(msgName);
    },
    close() {
        let msgName = this.getMsgName('close');
        window.nativeCall(msgName);
    },
    restore() {
        let msgName = this.getMsgName('restore');
        window.nativeCall(msgName);
    },
    isMaximized() {
            let hSpan = window.outerHeight - screen.availHeight
            let wSpan = window.outerWidth - screen.availWidth
            return Math.abs(hSpan) < 2 && Math.abs(wSpan) < 2
        },
    maximized: false,
    init() {
        window.addEventListener('resize', () => {
            let curState = this.isMaximized()
            let oldState = this.maximized
            this.maximized = curState
            if (oldState && !curState) eventer.emit(`window_unMaximize`)
            else if (!oldState && curState) eventer.emit(`window_maximize`)
        })
    }
}
let minimizeBtn = document.querySelector("#minimizeBtn");
let maximizeBtn = document.querySelector("#maximizeBtn");
let restoreBtn = document.querySelector("#restoreBtn");
let closeBtn = document.querySelector("#closeBtn");
minimizeBtn.addEventListener("click", () => browserWindow.minimize());
closeBtn.addEventListener("click", () => browserWindow.close())
maximizeBtn.addEventListener("click", () => {
    browserWindow.maximize();
    maximizeBtn.setAttribute("style", "display:none");
    restoreBtn.removeAttribute("style");
})
restoreBtn.addEventListener("click", () => {
    browserWindow.restore();
    restoreBtn.setAttribute("style", "display:none");
    maximizeBtn.removeAttribute("style");
})
browserWindow.init();
eventer.on("window_maximize", () => {
    maximizeBtn.setAttribute("style", "display:none");
    restoreBtn.removeAttribute("style");
})
eventer.on("window_unMaximize", () => {
    restoreBtn.setAttribute("style", "display:none");
    maximizeBtn.removeAttribute("style");
})